package com.gdth.bank.adapter;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.bumptech.glide.Glide;
import com.gdth.bank.R;
import com.gdth.bank.bean.DownloadBean;
import com.gdth.bank.model.DownloadModel;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.model.Item;
import com.gdth.utils.ToolUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GmJjDjListViewAdapter  extends BaseAdapter {

	private int layout_id;
    private LayoutInflater layoutInflater;  
    private Context context;  
    
 	private List<TAppFjxxb> valList;
 	
 	private List<Item> fjlxList;

    public GmJjDjListViewAdapter(Context context, List<TAppFjxxb> valList) {  
        this.context = context;  
        this.valList = valList;
        
        layout_id = R.layout.item_gmjj_fjxx;
    }  

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return valList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
        final TAppFjxxb item = valList.get(position);
        if (convertView == null) {  
            layoutInflater = LayoutInflater.from(context);  
            convertView = layoutInflater.inflate(layout_id, null);  
            holder = new Holder();  
            holder.txt_fjmc = (TextView) convertView.findViewById(R.id.fjxx_item_fjmc);
            holder.txt_fjlx = (TextView) convertView.findViewById(R.id.fjxx_item_fjlx);
            holder.img = (ImageView) convertView.findViewById(R.id.fjxx_item_img);
            holder.delete = (TextView)convertView.findViewById(R.id.delete_button);
            if(this.mOnDeleteListener!=null){
            	holder.delete.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if(item.getSczt()!=1){
						mOnDeleteListener.clickListener(position);}
					}
				});
            }
            
            convertView.setTag(holder);  
        } else {  
            holder = (Holder) convertView.getTag();  
        }  
        if (holder != null) {  
            convertView.setId(position);  
            holder.setId(position);  
            holder.txt_fjmc.setText(item.getWjmc());  
            holder.txt_fjmc.setTag(item.getUuid());
            if(item.getSczt()==1){
            	 holder.delete.setText("");
            }else{
            holder.delete.setText("删除");}
            if (fjlxList != null) {
				for (Item mItem : fjlxList) {
					if (mItem.getKey().equals(item.getFjlx().toString())) {
						if(!mItem.getValue().toString().equals("")){
						holder.txt_fjlx.setText(mItem.getValue());
						}else{
							holder.txt_fjlx.setText("");
						}
						break;
					}
				}
			}
            String fullPath = ToolUtil.getFileFullPath(ToolUtil.getFilePath(item.getUuid() + "." +item.getWjlx()));
            File file = new File(fullPath);
            holder.img.setImageResource(R.drawable.bank_tx);
            if(file.exists()) {
            	Glide.with(context).load(fullPath).into(holder.img);
            }else {
            	Bitmap bm = BitmapFactory.decodeFile(fullPath);
            	if (bm == null) {
    				DownloadBean bean = new DownloadBean();
    				bean.setUuid(item.getUuid());
    				bean.setLocalPath(item.getAppdz());
    				bean.setGnmk(item.getGnmk());
    				bean.setCjsj(new Date());
    				DownloadModel.getInstance().download(bean, holder.img, handler);
    			}
            	
            	         	
            }
	    }
        return convertView;  
	}
	 public class Holder {  
	    	public TextView txt_fjmc;   
	    	public TextView txt_fjlx;
	    	public ImageView img;
	    	public TextView delete;
	    	
	    	void setId(int position) {  
	    		txt_fjmc.setId(position);    
	    	}  
	    }
	    
	    public void setFjlx(List<Item> fjlxList) {
			this.fjlxList = fjlxList;
		}
	    
	    Handler handler = new Handler(new Handler.Callback() {
			
			@Override
			public boolean handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch (msg.what) {
					case 101:
						notifyDataSetChanged();
						break;
				}
				
				return false;
			}
		});
	    
	    
	    private OnDeleteListener mOnDeleteListener;
	  
	    
	    public void setmOnDeleteListener(OnDeleteListener mOnDeleteListener) {
			this.mOnDeleteListener = mOnDeleteListener;
		}
		public interface OnDeleteListener{
	    	void clickListener(int position);
	    }
}
